<?php


namespace app\home\controller;


use app\base\BaseController;
use think\facade\Db;
use think\facade\View;

/**
 * Class VisitRecord
 * @package app\home\controller
 * 拜访记录
 */
class VisitRecord extends BaseController
{
    protected $title = '拜访记录';
    protected $table = 'visit_record';

    public function index()
    {
        if (request()->isAjax()) {
            $param = get_params();
            $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
            $where[] = ['vr.status', '>', -1];
            $haveAuth=false;
            if ($this->isHaveAuth('see_all')){
                $haveAuth=true;
            }elseif($this->isHaveAuth('see_depart')){
                $haveAuth=true;
                $where[]=['vr.departId','in',$this->dSons];
            }elseif($this->isHaveAuth('see_self')){
                $haveAuth=true;
                $where[]=['vr.createId','=',$this->uid];
            }
            if (!$haveAuth){
                return table_assign(1,'暂无查看权限');
            }
            if (isset($param['uid'])&&$this->param['uid']){
                $where[]=['vr.saleId','=',$param['uid']];
            }
            if (isset($param['pName'])&&$this->param['pName']){
                $where[]=['p.name','like','%'.$param['pName']."%"];
            }
            if (isset($param['id'])&&$this->param['id']){
                $where[]=['vr.id','=',$param['id']];
            }
            if (isset($param['apply_time'])&&$this->param['apply_time']){
                $times=explode('~',$this->param['apply_time']);
                $where[]=['vTime','between',$times];
            }

            if (isset($param['text'])&&$this->param['text']){
                $where[]=['vr.vJob|vr.vName|vr.vType|vr.content','like','%'.$param['text']."%"];
            }
            $list = Db::name($this->table)
                ->alias('vr')
                ->leftJoin('purveyor p', 'p.id=vr.purveyorId')
                ->leftJoin('admin ad', 'ad.id=vr.saleId')
                ->field('p.name as pName,ad.name as saleUser,vr.*')
                ->where($where)
                ->order('vr.id DESC')
                ->paginate($rows);
            return table_assign(0, '', $list);
        } else {
            $punterList = Db::name('purveyor')->where('status', 1)
                ->where('dw_type', 2)
                ->field('id,name')->select();
            $adminList = Db::name('admin')->where('status', 1)->field('id,name')->select();
            view::assign(['punterList' => $punterList, 'adminList' => $adminList]);
            return view();
        }
    }

    public function add()
    {
        $param = get_params();
        if (request()->isAjax()) {
            $param = $this->param;
            $param['update_time'] = time();
            if ($param['id'] > 0) {
                Db::name($this->table)->strict(false)->field(true)->update($param);
                add_log('edit', $param['id'], $param, $this->title);
            } else {
                $param['createId'] = $this->uid;
                $param['departId'] = $this->did;
                $param['create_time'] = time();
                $rid = Db::name($this->table)->strict(false)->field(true)->insertGetId($param);
                add_log('add', $rid, $param, $this->title);
            }
            return to_assign();
        } else {
            $id = isset($param['id']) ? $param['id'] : 0;
            if ($id > 0) {
                $detail = Db::name($this->table)->where('id', $id)->find();
                View::assign('detail', $detail);
            }
            $punterid=Db::name('purveyor')->where('status', 1)
                ->where('dw_type', 2)
                ->where(function ($query){
                    $query->where('saleIds','=' ,$this->uid);
                    $query->whereOr('saleIds','like',$this->uid.',%');
                    $query->whereOr('saleIds','like','%,'.$this->uid.',%');
                    $query->whereOr('saleIds','like','%,'.$this->uid);
                    return $query;
                })->column('id');

            $punterList = Db::name('purveyor')->where('status', 1)
                ->where('dw_type', 2)
                ->where('id','in',$punterid)
                ->field('id,name')->select();
            $adminList = Db::name('admin')
//                ->where('id', '>', 1)
                ->where('status', 1)
                ->field('id,name')
                ->select();
            View::assign('adminList', $adminList);
            View::assign('punterList', $punterList);
            View::assign('id', $id);
            return view('form');
        }
    }

    public function delete()
    {

        $id = get_params("id");
        try {
            Db::name($this->table)->where('id', 'in', $id)->update(['status' => -1, 'deleted' => time()]);
            add_log('delete', $id, request()->param(), $this->title);
        } catch (\Exception $e) {
            return to_assign(1, "删除失败" . $e->getMessage());
        }
        return to_assign(0, "删除成功");
    }
}